<?php
class system_user_concept extends system_concept_abstract {
	
	public function init() {
		$this->setRowClass('system_user_row');		
		$this->setModel('system_user_model', 'user');
		$this->setJoinModel('system_passport_model', 'user.id = passport.id', 'passport');
		$this->setJoinLeftModel('system_user_group_model', 'group.id = user.group_id', 'group');
		$this->setModelCols('user', array('id', 'lang', 'group_id', 'nickname', 'privilege', 'creationtime'));
		$this->setModelCols('passport', array('name', 'identity', 'credential', 'secure', 'email', 'creationip', 'lastupdate', 'lastupip'));
		$this->setModelCols('group', array('name' => 'group_name', 'privilege' => 'group_privilege', 'is_administrator'));
	}
	
	public function save($row) {
		if($row->getId() == null || intval($row->get('id')) == 0) {
			throw new system_exception("PASSPORT ID can not be empty.");
		}
		if(!$row->getPrivilege()) {
			$row->setPrivilege(serialize(array()));
		}
		$row->set('id', intval($row->getId()));
		$passport = system_passport_instance::isExist($row->getId());		
		if($this->getModel('user')->fetchRow(array('id = ?' => $row->getId()))) {
			$this->getModel('user')->update($row->getModelData('user'), array('id = ?' => $row->getId()));			
		} else {
			$row->setCreationtime(system_date::timestamp());
			$this->getModel('user')->insert($row->getModelData('user'));
		}
		return $row;		
	}

	/**
	 * 密码验证
	 * @param system_user_row $login
	 * @param unknown_type $password
	 */
	public function auth(system_user_row $login, $password) {
		return $login->getCredential() == $password;
	}

}